﻿#region Usings

using System.Web.Mvc;
using Ninject;
using Omu.Awesome.Core;
using Rail.Net.Core.Interfaces.Data;
using Rail.Net.Core.Model;

#endregion

namespace Rail.Net.Areas.Admin.Controllers
{
    public abstract class ScaffoldController<T> : Controller where T : BaseIdentifiable
    {
        private const int PageSize = 50;

        [Inject]
        public IDalContext DalContext { get; set; }

        public abstract IRepository<T> Repository { get; }

        //
        // GET: /Entities/
        public ViewResult Index(int? page)
        {
            var pageIndex = page.HasValue ? page.Value : 0;
            var pagable = new Pageable<T>
                              {
                                  Page = Repository.All(pageIndex, PageSize),
                                  PageIndex = pageIndex,
                                  PageCount = PageSize
                              };

            return View(pagable);
        }

        //
        // GET: /Entities/Details/5
        public ViewResult Details(long id)
        {
            return View(Repository.Find(id));
        }

        //
        // GET: /Entities/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Entities/Create
        [HttpPost]
        public ActionResult Create(T distance)
        {
            if (ModelState.IsValid)
            {
                Repository.Create(distance);
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Entities/Edit/5
        public ActionResult Edit(long id)
        {
            return View(Repository.Find(id));
        }

        //
        // POST: /Entities/Edit/5
        [HttpPost]
        public ActionResult Edit(T distance)
        {
            if (ModelState.IsValid)
            {
                Repository.Update(distance);
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Entities/Delete/5
        public ActionResult Delete(long id)
        {
            return View(Repository.Find(id));
        }

        //
        // POST: /Entities/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Repository.Delete(Repository.Find(id));

            return RedirectToAction("Index");
        }
    }
}